package cate.game.activity.warorder;

import cate.common.table.activity.warorder.row.WarorderBaseRow;
import cate.common.table.activity.warorder.row.WarorderMallRow;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.warorder.msg.WarOrderMallResp;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.List;

@NoteClass("战令商店数据")
public class RoleWarOrderMall extends SubActivitySingle<RoleWarOrder> {

	@NoteField("商品列表")
	public List<Item> items;

	public void initialize(RoleWarOrder parent) {
		super.initialize(parent);
		if (items == null) {
			items = new ArrayList<>();
		}
	}

	public void excelCheck() {
		List<Integer> itemIds = role.getGame().table.warorder.mall.getItemsByGroup(configGroup());
		for (Integer tid : itemIds) {
			Item item = getItem(tid);
			if (item == null) {
				item = new Item();
				item.tid = tid;
				items.add(item);
			}
		}
		items.removeIf(a -> !itemIds.contains(a.tid));
	}

	public void beforeOver() {

	}

	public void clear() {
		items.clear();
	}

	@JsonIgnore
	public int configGroup() {
		WarorderBaseRow baseRow = parent.getRow();
		if (baseRow == null) {
			return -1;
		} else {
			return baseRow.mallGroup;
		}
	}

	@JsonIgnore
	private long getMallExp() {
		return role.getBag().getBag(GDObj.Type.NUM).count(parent.getRow().mallExpItem);
	}

	@JsonIgnore
	private int getMallLevel(){
		return role.getGame().table.warorder.mallLevel.getLevel(getMallExp());
	}

	public GameResult buyItem(int tid, int num) {
		GameResult r = new GameResult();
		Item item = getItem(tid);
		if (item == null) {
			return r.fail("商品不存在");
		}
		WarorderMallRow itemRow = parent.getRole().getGame().table.warorder.mall.get(tid);
		if (itemRow == null) {
			return r.fail("商品不存在");
		}
		if (num <= 0) {
			return r.fail("购买数量有误");
		}
		if (itemRow.limit > 0 && item.buyNum > itemRow.limit - num) {
			return r.fail("已售罄");
		}

		if (itemRow.mallLevelRequired > 0 && itemRow.mallLevelRequired > getMallLevel()) {
			return r.fail("商城等级不足");
		}
		MixRes consume = new MixRes();
		consume.addItem(new MixResItem(GDObj.Type.PROP, parent.getRow().tokenItemId, itemRow.price * num));
		r = consume.consume(parent.getRole());
		if (!r.ok()) {
			return r;
		}
		item.buyNum += num;
		MixRes rwd = new MixRes(itemRow.rwdStr).multiply(num);
		EcResult exe = new MixResAdder().setRes(rwd).
				setOperation(GDOperation.A_WAR_ORDER_MALL).
				setOperationSub(tid).setDialogReward(true).exe(parent.getRole());
		noticeUpdate();
		return r.success();
	}


	public Item getItem(int tid) {
		for (Item item : items) {
			if (item.tid == tid) {
				return item;
			}
		}
		return null;
	}

	@NoteClass("商品信息")
	public static class Item{
		@NoteField("商品id")
		public int tid;

		@NoteField("购买数量")
		public int buyNum;
	}

	@Override
	public void onDaySpan(boolean silence) {
		boolean change = false;
		for (Item item : this.items) {
			if (item.buyNum > 0 && parent.getRole().getGame().table.warorder.mall.get(item.tid).dailyRefresh) {
				change = true;
				item.buyNum = 0;
			}
		}
		if (!silence && change) {
			noticeUpdate();
		}
	}

	public void noticeUpdate(){
		role.sendNow(new WarOrderMallResp(funcId(),this));
	}

}
